import 'package:flutter/material.dart';
import 'package:flutter_getx/view/home/logic.dart';
import 'package:flutter_getx/view/home/state.dart';
import 'package:get/get.dart';
import 'package:my_image/my_image.dart';

class HomeList extends StatelessWidget {
  const HomeList({super.key});

  @override
  Widget build(BuildContext context) {
    final HomeLogic logic = Get.put(HomeLogic());
    final HomeState state = Get.find<HomeLogic>().state;

    List<Widget> buildList() {
      return state.notifyList.map((item) {
        return GestureDetector(
          onTap: () {
            // Navigator.pushNamed(context, '/detail', arguments: {'id': '10011xx'});

            Get.toNamed('/detail', arguments: {'id': item.id} as Map<String, dynamic>);
          },
          child: Container(
            padding: const EdgeInsets.only(top: 10, bottom: 10),
            child: Container(
              padding: const EdgeInsets.all(10),
              margin: const EdgeInsets.only(top: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white,
              ),
              child: Column(
                children: [
                  Row(children: [
                    Expanded(
                      child: Text(
                        item.title,
                        style: const TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.bold,
                        ),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ]),
                  const SizedBox(height: 8),
                  Row(children: [
                    Expanded(
                      child: Text(
                        item.content,
                        maxLines: 3,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ]),
                  const SizedBox(height: 8),
                  Row(children: [
                    Expanded(
                      child: Text(
                        item.createdAt,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(color: Colors.grey),
                      ),
                    ),
                  ]),
                ],
              ),
            ),
          ),
        );
      }).toList();
    }

    return Container(
      margin: const EdgeInsets.only(top: 10),
      child: Column(
        children: [
          Row(
            children: [
              Image.asset(
                ImgRes.imagesPng('images/notice@2x.png'),
                width: 25,
              ),
              const Text(
                '社区',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
              const Text('公告', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.orange)),
            ],
          ),
          Obx(() {
            return ListView(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              children: buildList(), // 传递 state
            );
          })
        ],
      ),
    );
  }
}
